<template>
    <div class="majorMain">
        <el-row :gutter="10" class="mb8">
            <template v-if="is_super">
                <el-col :span="3">
                    <el-input
                        v-model="queryParams.tenant_name"
                        placeholder="请输入学校名称"
                        clearable
                        @keyup.enter="handleQuery"
                        @clear="handleQuery"
                    />
                </el-col>
                <el-col :span="1.5">
                    <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                </el-col>
            </template>
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
            </el-col>
        </el-row>

        <!-- 用户数据表格 -->
        <el-table :data="majorListData">
            <el-table-column type="selection" min-width="3%" align="center" />
            <el-table-column min-width="10%" label="专业编号" align="center" prop="id" :show-overflow-tooltip="true" />
            <el-table-column v-if="is_super" label="所属租户" align="center" prop="tenant_name" min-width="10%" />
            <!-- <el-table-column label="专业ID" align="center" prop="code" min-width="10%" /> -->
            <el-table-column label="专业名称" align="center" prop="name" min-width="10%" />

            <el-table-column label="专业排序" align="center" prop="sort" min-width="10%" />

            <el-table-column label="状态" align="center" min-width="10%">

                <template #default="scope">
                    <el-tag v-if="scope.row.status==0" type="primary">正常</el-tag>
                    <el-tag v-else type="danger">停用</el-tag>
                  
                </template>
            </el-table-column>

            <el-table-column label="备注" align="center" prop="notes" min-width="10%" />


            <el-table-column label="创建时间" align="center" prop="create_time" :show-overflow-tooltip="true"
                min-width="10%">
                <template #default="scope">
                    <span>{{ parseTime(scope.row.create_time) }}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" align="center" min-width="10%" class-name="small-padding fixed-width">
                <template #default="scope">
                    <div class="operate">
                        <div @click="upMajorOne(scope.row.id)" class="item">
                            <el-icon>
                                <EditPen />
                            </el-icon>修改
                        </div>

                        <div @click="delOneMajor(scope.row.id)" class="item">
                            <el-icon>
                                <Delete />
                            </el-icon>删除
                        </div>
                    </div>

                </template>
            </el-table-column>
        </el-table>
        <AddMajorDialog :title="title" :majorListData="majorListData" :majorId="majorId" ref="majorRef"
            @onAddSuccess="onAddSuccess" />
    </div>
</template>

<script setup>
import { ref } from "vue"
import { parseTime } from "@/utils/ruoyi"
import AddMajorDialog from "@/components/AddMajorDialog"
import { majorList, delMajor } from "@/api/admin/major"
import { ElMessage } from "element-plus";
import { useUserStore } from "@/store/modules/user.js";
const userStore = useUserStore()
const is_super=userStore.is_super
const majorRef = ref(null)
const majorId=ref(null)
const title=ref()
// 用户数据
const majorListData = ref([])

// 查询参数
const queryParams = ref({
    tenant_name: ''
})

// 搜索按钮操作
const handleQuery = () => {
    getMajorList()
}

// 新增专业
const handleAdd = () => {  
    majorId.value=null
    title.value="添加专业"
    majorRef.value.openMajorDialog = true
    majorRef.value.majorForm.name=""
    majorRef.value.majorForm.code=''
    majorRef.value.majorForm.sort=Math.max(...majorListData.value.map(obj => obj.sort))+1   
}
// 删除一条专业
const delOneMajor = (id) => {
    delMajor(id).then(res => {
        console.log(res);
        ElMessage.success("删除成功")
        getMajorList()
    })
}

//修改一条专业
const upMajorOne=(id)=>{
    title.value="修改专业"
    majorId.value=id
    majorRef.value.openMajorDialog = true
}


// 获取专业列表
const getMajorList = () => {
    console.log(queryParams.value);
    const params = is_super ? queryParams.value : {};
    majorList(params).then(res => {
        console.log(res);
        majorListData.value = res || []
    }).catch(err => {
        console.error(err)
        majorListData.value = []
    })
}
// 新增专业成功之后的回调
const onAddSuccess = () => {

    getMajorList()
}

getMajorList()
</script>

<style lang="scss" scoped>
.majorMain {
    padding-top: 30px;

    .operate {
        display: flex;
        justify-content: center;
        align-items: center;

        .item {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-right: 15px;
            color: #1890FF;
            cursor: pointer;
            font-size: 14px;
        }
    }
}
</style>